package tas.co.in.daoimpl.employees;

import java.util.List;
import java.util.StringTokenizer;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import freemarker.template.utility.ToCanonical;

import tas.co.in.dao.employees.DailyTaskConnect;
import tas.co.in.domain.employees.DailyTaskBean;

public class DailyTaskImpl extends SqlMapClientDaoSupport implements DailyTaskConnect 
{

	DailyTaskBean dailyTaskBean;
	
	TransactionTemplate template = null;

	public void setTemplate(TransactionTemplate template) {
		this.template = template;
	}
	
	/**
	 * @param dailyTaskBean the dailyTaskBean to set
	 */
	public void setDailyTaskBean(DailyTaskBean dailyTaskBean) {
		this.dailyTaskBean = dailyTaskBean;
	}
	

	public Boolean setTasks(final StringTokenizer tokenizer, final DailyTaskBean taskBean) {
		// TODO Auto-generated method stub

		boolean bool;
		
		bool = (Boolean) template.execute(new TransactionCallback(){

			public Boolean doInTransaction(TransactionStatus result) {
				
				boolean resultVar = false;
				Object savepoint = null;
				
				try {
					
					while (tokenizer.hasMoreTokens()) 
					{
						taskBean.setTaskID(tokenizer.nextToken().toString());
						taskBean.setTaskName(tokenizer.nextToken().toString());
						taskBean.setTaskAssignTo(tokenizer.nextToken().toString());
						
						taskBean.setTaskCity(tokenizer.nextToken().toString());	
						taskBean.setTaskCreateTime(tokenizer.nextToken().toString());
						taskBean.setTaskCreateDate(tokenizer.nextToken().toString());
						
						savepoint = result.createSavepoint();
						
						getSqlMapClientTemplate().insert("ScheduleTask.setDailyTask", taskBean);
						getSqlMapClientTemplate().update("ScheduleTask.setUpdateInRequests", taskBean);
					}
					
					resultVar = true;
					
					
					
				} catch (Exception e) {
					
					System.out.println(e);
					result.rollbackToSavepoint(savepoint);
					
				}
				
				return resultVar;
				
			}
			
		});
		
		
		return bool;

	}

	@SuppressWarnings("unchecked")
	public List<DailyTaskBean> getAllTaskList(DailyTaskBean bean) {
		// TODO Auto-generated method stub
		System.out.println("Here I");
		return getSqlMapClientTemplate().queryForList(
				"ScheduleTask.getAllTasks", bean);
	}

}
